import { InjectionKey } from "vue"
import { getLocal } from "./index"

export type AlertType = "warn" | "success" | "error"
export type AlertTarget = {
  show: boolean,
  type: AlertType,
  message: string
}

export const alertTarget = reactive<AlertTarget>({
  show: false,
  type: "success",
  message: ""
})

export function showAlert(showType: AlertType, showMsg: string, showTime: number = 1000) {
  alertTarget.type = showType
  alertTarget.message = showMsg
  alertTarget.show = true

  setTimeout(() => {
    alertTarget.show = false
  }, showTime);
}

export function showSuccessAlert(cnMsg: string, enMsg: string) {
  const language = getLocal("__LANGUAGE__")
  language === "cn" ? showAlert("success", cnMsg) : showAlert("success", enMsg)
}

export function showErrorAlert(cnMsg: string, enMsg: string) {
  const language = getLocal("__LANGUAGE__")
  language === "cn" ? showAlert("error", cnMsg) : showAlert("error", enMsg)
}

export const showAlertKey: InjectionKey<typeof showAlert> = Symbol("showAlert")
